package com.yanqu.road.server.cmd.crossunion;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.logic.cross.CrossUnionActivityRankListModel;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = CrossProtocol.C_CROSS_UNION_RANK_REWARD, desc = "")
public class CrossUnionRankRewardCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossUnionActivityRankDataProto.CrossUnionRankRewardReqMsg req = CrossUnionActivityRankDataProto.CrossUnionRankRewardReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        long userId = packet.getPlayerId();
        UnionActivityConditionData conditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, 0, userId);
        CrossUnionActivityRankDataProto.CrossUnionRankRewardRespMsg.Builder resp = CrossUnionActivityRankDataProto.CrossUnionRankRewardRespMsg.newBuilder();
        resp.setMyRank(-1);
        resp.setPosition(0);
        resp.setActivityId(activityId);
        if (conditionData != null) {
            CrossUnionActivityRankListModel model = ActivityRankMgr.getCrossActivityUnionRankListModel(activityId);
            int myRank = model.getMyRank(conditionData.getUnionUid());
            resp.setMyRank(myRank);
            Integer value = conditionData.getMemberPositionMap().get(userId);
            if (value != null) {
                resp.setPosition(value);
            }
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_SYNC_CROSS_UNION_RANK_REWARD, resp);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
